<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.428.0.min.js"></script>
<script src="${ctx}/static/js/jquery/jquery-1.8.3.min.js"></script>
<script src="${ctx}/static/js/s3.js"></script>
<script src="${ctx}/static/js/config.js"></script>
<script>
var userid;
var baseUrl='${ctx}';
function login(){
	$.ajax({
		  type: 'POST',
		  data:{
			  password: '111111',
			  phone: '15093359880',
		  },
		  url:baseUrl+'/u/login',
		  success: function(data,status,xhr){
			  if(data.code == 1){
				  userid = data.obj.userid;
				  var xAuthToken = xhr.getResponseHeader("xAuthToken");
				  console.log("login xAuthToken:"+xAuthToken);
				  Config.init({
				  	baseUrl: baseUrl, 
				  	xAuthToken: xAuthToken
				  });
				  S3Util.init();
			  }else{
				  console.log("login error:"+data);
			  }
		  }
	});
}
S3Util.onerror = function(data) {
	 console.log("onerror:"+data);
}
login();

function uploadFile(){
	var file = document.getElementById("file").files[0];
	var url = S3Util.upload(file,"userinfo/"+userid+"/",function(err,data){
		  if (err) {
		    console.log("Error", err);
		    alert('上传失败');
		  }else{
		    console.log("Upload Success", data.Location);
			var img ="<img src='"+data.Location+"'></img>";
			console.log(img);
			document.body.innerHTML += img;
		  }
	});
}

</script>
<title>s3测试</title>

</head>

<body>
<input id="file" type="file" /><br />
<input type="button" onclick="uploadFile()" value="上传" />
</body>
</html>